﻿
namespace MyFurion.Application.Controller
{
    /// <summary>
    /// 公告
    /// </summary>
    public class NoticeController:IDynamicApiController
    {
        private readonly NoticeRepository _repository;
        public NoticeController(NoticeRepository repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task Add(NoticeInfoAdd input)
        {
            var model = input.Adapt<NoticeInfo>();
            model.IsPublish =0;
            bool result=await _repository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 新增发布
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("addPublish")]
        public async Task AddPublish(NoticeInfoAdd input)
        {
            var model = input.Adapt<NoticeInfo>();
            model.IsPublish = 1;
            model.PublishDate= DateTime.Now;
            bool result = await _repository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task Update(NoticeInfoUpdate input)
        {
            var model =await _repository.GetEntityById(input.Id);
            if (model == null)
            {
                throw Oops.Oh(ErrorMsgConst.NoRecord);
            }
            model.Title = input.Title;
            model.NoticeContent= input.NoticeContent;
            bool result = await _repository.Update(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task Delete([FromQuery]BaseIdInput input)
        {
            bool result = await _repository.DeleteById(input.Id);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("batchDelete")]
        public async Task BatchDelete([FromQuery]BaseIdsInput input)
        {
            bool result = await _repository.DeleteByWhere(it=>input.Ids.Contains(it.Id));
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("publish")]
        public async Task Publish(BaseIdInput input)
        {
            bool result = await _repository.Update(it => it.Id == input.Id, it => new NoticeInfo() { IsPublish = 1, PublishDate = DateTime.Now });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
        }
        /// <summary>
        /// 撤销发布
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("cancelPub")]
        public async Task CancelPublish(BaseIdInput input)
        {
            bool result = await _repository.Update(it => it.Id ==input.Id, it => new NoticeInfo() {IsPublish=0,PublishDate=null });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("page")]
        public async Task<PageResult<NoticeInfo>>QueryPage(NoticeInfoPage input)
        {
            return await _repository.GetPageList(input);
        }
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<List<NoticeInfo>> QueryList(NoticeInfoPage input)
        {
            return await _repository.GetDataList(input);
        }
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<NoticeInfo> GetDetail(long id)
        {
            return await _repository.GetEntityById(id);
        }
    }
}
